package com.bhs.sansong.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.bhs.sansong.R;
import com.bhs.sansong.net.NetApi;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.net.RetrofitUtils;
import com.bhs.sansong.ui.login.PhoneNumberLoginActivity;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.StatusBarUtil;
import com.bhs.sansong.utils.ToastUtil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.pgyer.pgyersdk.PgyerSDKManager;


import org.json.JSONObject;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class BaseActivity extends AppCompatActivity {

    public NetApi netApi;
    public Context mContext;
    public BaseActivity mActivity;
    public LoadingPopupView loadingPopup;
    private boolean isShow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initLayout());
        AppManager.getInstance().addActivity(this);
        initialize();
        initView();
        initView(savedInstanceState);
        initData();

    }

    protected void initialize() {
        mContext = this;
        mActivity = this;
        //创建网络请求接口对象实例
        netApi = RetrofitUtils.getInstance().getRetrofit().create(NetApi.class);
        //设置状态栏
        StatusBarUtil.setTranslucentStatus(this, false);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
        StatusBarUtil.setStatusBarColor(this, 0xffffffff);
    }

    public void networkRequest(Call<ResponseBody> dataCall, NetCallBack callBack) {
        networkRequestCode(dataCall, callBack, 0);
    }

    /**
     * 假如一个界面有两个网络请求传入requestCode用于区分
     */
    public void networkRequest(Call<ResponseBody> dataCall, NetCallBack callBack, int requestCode) {
        networkRequestCode(dataCall, callBack, requestCode);
    }

    private void networkRequestCode(Call<ResponseBody> dataCall, NetCallBack callBack, int requestCode) {
        //异步请求
        dataCall.enqueue(new Callback<ResponseBody>() {
            //请求成功回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.code() == 200) {
                    try {
                        String body = response.body().string();
                        JSONObject jsonObject = new JSONObject(body);
                        String code = jsonObject.getString("code");
                        if (!code.equals("404")) {
                            callBack.onResponse(requestCode, body, response);
                        } else {
                            SharedPreferencesUtils.clear(mContext, "token");
                            startActivity(new Intent(mActivity, PhoneNumberLoginActivity.class));
                        }
                    } catch (Exception e) {
                        PgyerSDKManager.reportException(e);
                        e.printStackTrace();
                    }
                } else {
                    callBack.onFailure(requestCode);
                    ToastUtil.show("网络错误" + response.code() + response.message());
                }

            }

            //请求失败回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callBack.onFailure(requestCode);
                ToastUtil.show("网络错误2" + t.toString());
                loadingPopupDismiss();
            }
        });
    }

    /**
     * 设置根布局
     */
    public abstract int initLayout();

    /**
     * 初始化布局
     */
    public abstract void initView();

    public void initView(@Nullable Bundle savedInstanceState) {
        FrameLayout flBack = findViewById(R.id.fl_back);
        if (flBack != null) {
            flBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        }
    }

    ;

    /**
     * 设置数据
     */
    public abstract void initData();

    public void loadingPopupShow() {
        if (loadingPopup == null) {
            loadingPopup = (LoadingPopupView) new XPopup.Builder(mContext)
                    .dismissOnBackPressed(false)
                    .isLightNavigationBar(true)
                    .asLoading("", LoadingPopupView.Style.ProgressBar)
                    .show();
        } else {
            loadingPopup.show();
        }
    }

    public void loadingPopupShow(String hint) {
        if (loadingPopup == null) {
            loadingPopup = (LoadingPopupView) new XPopup.Builder(mContext)
                    .dismissOnBackPressed(false)
                    .dismissOnTouchOutside(false)
                    .isLightNavigationBar(true)
                    .asLoading(hint, LoadingPopupView.Style.ProgressBar)
                    .show();
        } else {
            loadingPopup.setTitle(hint);
            loadingPopup.show();
        }
    }

    public void loadingPopupDismiss() {
        if (loadingPopup != null) {
            loadingPopup.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从栈中移除
        AppManager.getInstance().finishActivity(this);
    }
}
